import { Form, Input, Button, Checkbox } from 'antd';
import React, { useState, useEffect } from "react";
const Demo = () => {
    const [form] = Form.useForm(); // 引入Form使用
    // form.validateFields().then((values) => {
    const onFinish = (values) => {
        form.validateFields().then((values) => {
            console.log(values)
        })
        console.log('Success:', values);
    };
    const Posts = () => {
        form.validateFields().then((values) => {
            axios.post('http://localhost:8080/user', { loginname: values.name })
                .then((res) => {
                    console.log(res.data); // handle success 
                    if (res.data.password == values.Password) {
                        console.log("登录成功")
                    } else {
                        console.log("登录失败")
                    }
                })
                .catch((error) => {
                    console.log(error);     // handle network error
                })
        })
    }
    const Gets = () => {
        form.validateFields().then((values) => {
            axios.get('http://localhost:8080/user', { params: { loginname: values.name } })
                .then((res) => {
                    console.log("get", res.data); // handle success 
                })
                .catch((error) => {
                    console.log(error);     // handle network error
                })
        })
    }
    // const onFinishFailed = (errorInfo) => {
    //     console.log('Failed:', errorInfo);
    // };
    return (
        <Form
            form={form}
            name="basic"
            className='Form'
            labelCol={{
                span: 8,
            }}
            wrapperCol={{
                span: 16,
            }}
            onFinish={onFinish}
            onFinishFailed={onFinishFailed}
            autoComplete="off"
        >
            <Form.Item

                label="Username"
                name="username"
                rules={[
                    {
                        required: true,
                        message: 'Please input your username!',
                    },
                ]}
            >
                <Input />
            </Form.Item>

            <Form.Item
                label="Password"
                name="Password"
                rules={[
                    {
                        required: true,
                        message: 'Please input your Password!',
                    },
                ]}
            >
                <Input.Password />

            </Form.Item>
            <Form.Item
                wrapperCol={{
                    offset: 8,
                    span: 16,
                }}
            >
                <Button type="primary" htmlType="submit" onClick={() => Gets()}>
                    get
                </Button>
                <Button type="primary" htmlType="submit" onClick={() => Posts()}>
                    post
                </Button>
            </Form.Item>
        </Form>
    );
};
export default Demo